/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Buddies.java
 *
 * Created on 14/02/2009, 18:20:03
 */
package edu.upf.grupE.interftest;

import edu.upf.grupE.impl.GUILogic;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import org.jivesoftware.smack.RosterEntry;

/*import edu.upf.grupE.img.**/
;

/**
 *
 * @author Oscar
 */
public class Buddies extends javax.swing.JPanel {

    private Hashtable users;
    String me;
    private Hashtable chats = new Hashtable();

    /** Creates new form Buddies */
    public Buddies() {
    }

    public void cargaBuddies(String me) {
        this.me = me;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();

        DefaultListModel list= new DefaultListModel();
        jList1.setModel(list);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/user_online.gif"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 599, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        // TODO add your handling code here:
        JList jList1 = (JList) evt.getSource();
        if (evt.getClickCount() == 2) {
            int index = jList1.locationToIndex(evt.getPoint());
            if (index >= 0) {
                String o = (String) jList1.getModel().getElementAt(index);





                JPanel conv = new JPanel(new BorderLayout());
//
                if (jTabbedPane1.indexOfTab(o) == -1) {
                    if (!chats.isEmpty() & chats.containsKey(o)) {
                        chats.remove(o);
                    }

                    Chat c = new Chat(me, o);
                    chats.put(o, c);
                    jTabbedPane1.add(o, c);
                    jTabbedPane1.setTabComponentAt(jTabbedPane1.indexOfTab(o), new TabComponent(o, jTabbedPane1));
                }




            }
        }
    }//GEN-LAST:event_jList1MouseClicked
    public void findUser(String email, String text) {
        System.out.println(email);
        int index = email.indexOf("/");

        String newemail = email.substring(0, index);
        System.out.println(newemail);
        if (jTabbedPane1.indexOfTab(newemail) == -1) {
            if (!chats.isEmpty() & chats.containsKey(newemail)) {
                chats.remove(newemail);
            }

            Chat c = new Chat(me, newemail);
            chats.put(newemail, c);
            jTabbedPane1.add(newemail, c);
            jTabbedPane1.setTabComponentAt(jTabbedPane1.indexOfTab(newemail), new TabComponent(newemail, jTabbedPane1));
        }

        Chat auxiliar = (Chat) chats.get(newemail);
        auxiliar.PrintMessage(newemail, text);

    }

    public void updateUsers() {
        DefaultListModel list = new DefaultListModel();
        Vector<RosterEntry> emails = GUILogic.getOnlineContacts();
        for (int i = 0; i < emails.size(); i++) {
            list.addElement(emails.get(i).getUser());
        }
        jList1.setModel(list);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}
